package com.filex.ads.interstitial;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.filex.ads.banner.FilexWebView;
import com.filex.ads.banner.HtmlWebViewListener;
import com.filex.ads.common.AdError;
import com.filex.ads.common.utils.DeviceUtils;


import static com.filex.ads.common.BaseBroadcastReceiver.sendBroadcastAction;
import static com.filex.ads.common.CommonKeys.AD_HEIGHT;
import static com.filex.ads.common.CommonKeys.AD_WIDTH;
import static com.filex.ads.common.CommonKeys.BROADCAST_EVENT_ID_KEY;
import static com.filex.ads.common.CommonKeys.HTML_RESPONSE_BODY_KEY;
import static com.filex.ads.common.IntentActions.ACTION_INTERSTITIAL_CLICK;
import static com.filex.ads.common.IntentActions.ACTION_INTERSTITIAL_DISMISS;
import static com.filex.ads.common.IntentActions.ACTION_INTERSTITIAL_SHOW;

public class InterstitialActivity extends BaseInterstitialActivity {

    public static void start(Context context, String htmlBody,  int adWidth, int adHeight, long broadCastEventId) {
        Intent intent = createIntent(context, htmlBody, adWidth, adHeight, broadCastEventId);
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException anfe) {
            Log.d("InterstitialActivity", anfe.getMessage());
        }
    }

    static Intent createIntent(Context context, String htmlData, int adWidth, int adHeight, long broadCastEventId) {
        Intent intent = new Intent(context, InterstitialActivity.class);
        intent.putExtra(HTML_RESPONSE_BODY_KEY, htmlData);
        intent.putExtra(BROADCAST_EVENT_ID_KEY, broadCastEventId);
        intent.putExtra(AD_WIDTH, adWidth);
        intent.putExtra(AD_HEIGHT, adHeight);
        if (! (context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    @Override
    public View getAdView() {
        Intent intent = getIntent();
        String htmlResponse = intent.getStringExtra(HTML_RESPONSE_BODY_KEY);
        FilexWebView filexWebView = new FilexWebView(this);
        filexWebView.init(new InterstitialHtmlWebViewListener());
        filexWebView.loadHtmlResponse(htmlResponse);
        return filexWebView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DeviceUtils.lockOrientation(this);

        sendBroadcastAction(this, getBroadcastIdentifier(), ACTION_INTERSTITIAL_SHOW);
    }

    @Override
    protected void onDestroy() {
        sendBroadcastAction(this, getBroadcastIdentifier(), ACTION_INTERSTITIAL_DISMISS);
        super.onDestroy();
    }

    class InterstitialHtmlWebViewListener implements HtmlWebViewListener {

        @Override
        public void onLoaded() {
        }

        @Override
        public void onFailed(AdError adError) {

        }

        @Override
        public void onClicked() {
            sendBroadcastAction(InterstitialActivity.this, getBroadcastIdentifier(), ACTION_INTERSTITIAL_CLICK);
        }
    }


}
